{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Python 递归函数\n",
    "\n",
    "在函数内部，可以调用其他函数。如果一个函数在内部调用自身本身，这个函数就是递归函数。\n",
    "\n",
    "可视化过程链接： http://www.pythontutor.com/visualize.html#mode=edit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def fact(n):\n",
    "    if n==1:\n",
    "        return 1\n",
    "    return n * fact(n - 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "fact(1) = $1$\n",
    "\n",
    "fact(2) = $1*2 = 2$\n",
    "\n",
    "fact(3) = $3*2 = 6$\n",
    "\n",
    "fact(4) = $4*3*2 = 4*6 = 24$\n",
    "\n",
    "fact(5) = $5*4*3*2 = 5*24 = 120$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "120\n"
     ]
    }
   ],
   "source": [
    "print(fact(5))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EMA计算\n",
    "\n",
    "**$EMA_t=α*Price_t + ( 1 - α ) * EMA_{t-1}$**\n",
    "\n",
    "$α为平滑指数，一般取作2/(n+1)$\n",
    "\n",
    "1. 自定义方法\n",
    "2. Pandas递归函数\n",
    "3. talib的EMA"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. 自定义方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    0.000000\n",
      "1    0.285714\n",
      "2    0.775510\n",
      "3    1.411079\n",
      "4    2.150771\n",
      "5    2.964836\n",
      "6    3.832026\n",
      "7    4.737161\n",
      "8    5.669401\n",
      "9    6.621001\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "a = 2/7\n",
    "Prices = list(range(10)) #prices\n",
    "def ema ( N , Price) :\n",
    "    if N < 1:\n",
    "        return Price[0]\n",
    "    return (1-a)*ema(N-1,Price) + a*Price[N]\n",
    "\n",
    "print (pd.Series([ema(i,Prices) for i in range(10)]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. Pandas递归函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       test\n",
      "0  0.000000\n",
      "1  0.285714\n",
      "2  0.775510\n",
      "3  1.411079\n",
      "4  2.150771\n",
      "5  2.964836\n",
      "6  3.832026\n",
      "7  4.737161\n",
      "8  5.669401\n",
      "9  6.621001\n"
     ]
    }
   ],
   "source": [
    "recursion_df = pd.DataFrame({'test': Prices})\n",
    "recursion_result = recursion_df.ewm(alpha=2/7, adjust=False).mean()\n",
    "print(recursion_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. talib的EMA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    NaN\n",
      "1    NaN\n",
      "2    NaN\n",
      "3    NaN\n",
      "4    NaN\n",
      "5    2.5\n",
      "6    3.5\n",
      "7    4.5\n",
      "8    5.5\n",
      "9    6.5\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "import talib as ta\n",
    "import numpy as np\n",
    "\n",
    "EMA = pd.Series(ta.EMA(np.array(Prices, dtype=np.float64),6))\n",
    "print(EMA)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
